package pl.druminski.robinhood.controller;

import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import pl.druminski.robinhood.model.Data;
import pl.druminski.robinhood.view.Gui;

/**
 * @author Łukasz Drumiński
 * 
 */
public class ALAutostart implements ActionListener
{

    Gui gui;
    Data data;

    public ALAutostart(Data data, Gui gui)
    {
        this.data = data;
        this.gui = gui;
    }

    public void actionPerformed(ActionEvent e)
    {
        if (gui.chckBoxAutostart.isSelected())
        {
            //program ma sie uruchamiac przy starcie systemu
            autostart(true);
        } else
        {
            //program nie ma uruchamiac sie przy starcie systemu
            autostart(false);
        }
    }

    public void autostart(Boolean start)
    {
        String osName = System.getProperty("os.name");
        if (osName.indexOf("Windows") >= 0)
        try
        {
            RegistryKey regkey = Registry.HKEY_LOCAL_MACHINE;
            RegistryKey key = Registry.openSubkey(regkey, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKey.ACCESS_ALL);

            data.setRunWithSystemStart(start);
            if (start)
            {
                RegStringValue stringValue = new RegStringValue(key, "RobinHood", RegistryValue.REG_SZ);
                stringValue.setData(data.getRootPath() + "RobinHood.exe");
                key.setValue(stringValue);
            }
            else
            {
                key.deleteValue("RobinHood");
            }

        } catch (Exception ex)
        {
            Logger.getLogger(data.getLogName()).warning("Problem z rejestrem: " + ex.getMessage());
        }

    }
}
